package br.mikhas.lab;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class ToHex {

	private String[] hex = new String[256];

	private String getHexa(byte b) {
		int index = b & 0xFF;

		String h = hex[index];
		if (h == null) {
			h = Integer.toHexString(index).toUpperCase();
			if (h.length() == 1)
				h = '0' + h;
			hex[index] = h;
		}

		return h;
	}

	public void write(File in, File out) {
		try {
			FileInputStream input = new FileInputStream(in);
			if (out.exists())
				out.createNewFile();

			Writer output = new BufferedWriter(new FileWriter(out));

			byte[] buffer = new byte[1024];
			int read, i;

			while ((read = input.read(buffer)) > 0)
				for (i = 0; i < read; i++)
					output.write(getHexa(buffer[i]));

			input.close();
			output.close();
		} catch (IOException e) {

		}
	}

	public static void main(String[] args) {
		new ToHex().write(new File(args[0]), new File(args[1]));
	}
}
